package com.frontend.screens;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.frontend.LockoutException;
import com.frontend.NetworkInterfacer;
import com.users.*;

/**
 * 
 * @author Elizabeth
 * @author Taylor
 * 
 */
public class LoginScreen extends Screen {
	/**
	 * 
	 */
	private static final long serialVersionUID = 106254145478338621L;
	private JLabel message, usernameLabel, passwordLabel;
	private JPanel usernamePanel, passwordPanel;
	private JPanel b1, b2, b3, b4, b5;
	private JTextField username;
	private JPasswordField password;
	private JButton submit;
	private boolean block;
	private javax.swing.Timer timer;
	private int cycles;

	public LoginScreen(DoctorsOfficeClient window) {
		super(window);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		timer = new javax.swing.Timer(50, new AnimationListener());
		LoginListener ll = new LoginListener();
		b1 = new JPanel();
		b1.setOpaque(false);

		message = new JLabel("Welcome to the Doctor's Office!");
		message.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);

		usernamePanel = new JPanel();
		usernamePanel.setOpaque(false);
		usernamePanel.setLayout(new BoxLayout(usernamePanel, BoxLayout.X_AXIS));
		usernameLabel = new JLabel("Username:  ");
		username = new JTextField();
		username.addActionListener(ll);
		usernamePanel.add(usernameLabel);
		usernamePanel.add(username);

		b3 = new JPanel();
		b3.setOpaque(false);

		passwordPanel = new JPanel();
		passwordPanel.setOpaque(false);
		passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.X_AXIS));
		passwordLabel = new JLabel("Password:  ");
		password = new JPasswordField();
		password.addActionListener(ll);
		passwordPanel.add(passwordLabel);
		passwordPanel.add(password);

		b4 = new JPanel();
		b4.setOpaque(false);

		submit = new JButton("Login");
		submit.setOpaque(false);
		submit.setAlignmentX(Component.CENTER_ALIGNMENT);
		submit.addActionListener(ll);

		b5 = new JPanel();
		b5.setOpaque(false);

		add(b1);
		add(message);
		add(b2);
		add(usernamePanel);
		add(b3);
		add(passwordPanel);
		add(b4);
		add(submit);
		add(b5);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			usernameLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			username.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			username.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			passwordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			password.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			password.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			submit.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			submit.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	private class LoginListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			NetworkInterfacer net = window.getNet();
			String name = username.getText();
			String pwd = new String(password.getPassword());
			block = false;

			if (name.equals("")) {
				block = true;
				showError("Username field cannot be blank");
			} else if (pwd.equals("")) {
				block = true;
				showError("Password field cannot be blank");
			}
			if (!block) {
				boolean lockout = false;

				User current = null;
				try {
					current = net.login(name, pwd);
				} catch (ConnectionException sqe) {
					try {
						try {
							net.reconnect();
						} catch (ConnectionException e1) {
							System.out.println(e1.getMessage());
						}
						current = net.login(name, pwd);
					} catch (ConnectionException sqe2) {
						System.out.println(sqe.getMessage());
					} catch (LockoutException le) {
						lockout = true;
						showError("You have been locked out.");
					}
				} catch (LockoutException le) {
					lockout = true;
					showError("You have been locked out.");
				}
				if (!lockout) {
					if (current == null) {
						showError("Incorrect Login Information");
					} else {
						window.setCurrentUser(current);
						window.changeScreen(new NavigationScreen(window));
					}
				}
			}
		}
	}
	
	private void showError(String line){
		timer.stop();
		block = true;
		cycles = 0;
		message.setText(line);
		message.setForeground(Color.red);
		timer.start();
	}

	private class AnimationListener implements ActionListener {
		private int alphaVal;
		private boolean fadeout, fadein;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cycles == 0){
				alphaVal = 255;
				fadeout = true;
				fadein = false;
			}
			cycles++;

			if (cycles > 20) {
				if (fadeout) {
					alphaVal = alphaVal - 10;
					if (alphaVal < 0)
						alphaVal = 0;
					message.setForeground(new Color(255, 0, 0, alphaVal));
					if (alphaVal == 0) {
						fadeout = false;
						fadein = true;
						message.setText("Welcome to the Doctor's Office!");
					}
				}
				if (fadein) {
					alphaVal += 10;
					if (alphaVal > 255)
						alphaVal = 255;
					message.setForeground(new Color(0, 0, 0, alphaVal));
					if (alphaVal == 255) {
						fadein = false;
						timer.stop();
					}
				}
			}
		}

	}

}